﻿using AutoMapper;
using BW.MesProject.Production.Domain;
using BW.MesProject.Production.Domain.Dto.Input;
using BW.MesProject.Production.Domain.Dto.Input.ProductLock;
using BW.MesProject.Production.Domain.Dto.Output.ProductLock;
using BW.MesProject.Production.Infrastructure;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BW.MesProject.Production.Application
{
    public class ProductLockServices : IProductLockServices
    {
        private readonly IBaseRepository<PRODUCTLOCKMODEL> _productLockRepository;
        private readonly IMapper _mapper;
        private readonly IBaseRepository<WORKORDERMODEL> _workOrderRepository;
        public ProductLockServices(IBaseRepository<PRODUCTLOCKMODEL> productLockRepository, IMapper mapper, IBaseRepository<WORKORDERMODEL> workOrderRepository)
        {
            _productLockRepository = productLockRepository;
            _mapper = mapper;
            _workOrderRepository = workOrderRepository;
        }

        public ApiResult<int> CreateProductLock(ProductLockInput input)
        {
            int result = 0;
            var codes = input.PRODUCTLOCKCODE.Split(',');
            for (int i=0;i<codes.Length;i++)
            {
                PRODUCTLOCKMODEL entity = new PRODUCTLOCKMODEL();
                entity.PRODUCTLOCKTYPE = input.PRODUCTLOCKTYPE;
                entity.PRODUCTLOCKPRODUCTION = input.PRODUCTLOCKPRODUCTION;
                entity.PRODUCTLOCKBEGINTIME = input.PRODUCTLOCKBEGINTIME;
                entity.PRODUCTLOCKENDTIME = input.PRODUCTLOCKENDTIME;
                entity.LOCKINGREASON = input.LOCKINGREASON;
                entity.PRODUCTLOCKCODE = codes[i];
                entity.CREATETIME = DateTime.Now;
                entity.ISDELETED = false;
                entity.CREATEUSERNAME = "admin";
                result += _productLockRepository.Add(entity);
            }
            

            if (result > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Message = "产品锁定成功",
                    Data = result
                };
            }
            return new ApiResult<int>
            {
                Code = ApiEnums.Error,
                Message = "产品锁定失败",
                Data = result
            };
        }

        public ApiResult<int> DeleteProductUpLock(string code)
        {
            var productlocks = _productLockRepository.GetAll();
            var dev = code.Split(',');
            foreach (var item in dev)
            {
                var productlock = productlocks.First(x => x.PRODUCTLOCKCODE == item && !x.ISDELETED);
                if (productlock == null)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Message = $"产品锁定数据不存在，请检查输入的编码是否正确: {item}",
                        Data = 0
                    };
                }
                _productLockRepository.Delete(productlock);
            }
           
            return new ApiResult<int>
            {
                Code = ApiEnums.Success,
                Message = "产品锁定解锁成功",
                Data = 1
            };
        }

        public ApiResult<List<ProductLockOutput>> GetProductLocks(ListInput input)
        {
            var query = new List<WORKORDERMODEL>();
            foreach(var item in input.Code.Split(','))
            {
                var dd = _workOrderRepository.GetAll().First(x => x.WORKORDERCODE == item && !x.ISDELETED);
                query.Add(dd);
            }

            if (query == null)
            {
                if (input.Type == "主数据")
                {
                    return new ApiResult<List<ProductLockOutput>>
                    {
                        Code = ApiEnums.Fail,
                        Message = "不存在产品主数据的数据, 请检查输入的编码是否正确或者编码主数据类型是否为半成品/成品!",
                        Data = null
                    };
                }
                if (input.Type == "工单")
                {
                    return new ApiResult<List<ProductLockOutput>>
                    {
                        Code = ApiEnums.Fail,
                        Message = "不存在工单数据, 请检查输入的编码是否正确!",
                        Data = null
                    };
                }
                if (input.Type == "产品")
                {
                    return new ApiResult<List<ProductLockOutput>>
                    {
                        Code = ApiEnums.Fail,
                        Message = "不存在产品数据, 请检查输入的编码是否正确!",
                        Data = null
                    };
                }
            }
            var unlock = new PRODUCTLOCKMODEL();
            string codes = null;
            for(int i=0;i<query.Count();i++)
            {
                unlock = _productLockRepository.GetAll()
                .First(x => x.PRODUCTLOCKCODE == input.Code && !x.ISDELETED);
               
            }
            if (codes != null)
            {
                return new ApiResult<List<ProductLockOutput>>
                {
                    Code = ApiEnums.Fail,
                    Message = $"以下工单编码已在锁定状态中:【{codes}】",
                    Data = null
                };
            }
            var outputs = new List<ProductLockOutput>();
            foreach(var item in query)
            {
                outputs.Add(new ProductLockOutput()
                {
                    Code = item.WORKORDERCODE,
                    Type = input.Type,
                });
            }
            return new ApiResult<List<ProductLockOutput>>
            {
                Code = ApiEnums.Success,
                Message = "产品锁定列表获取成功",
                Data = outputs
            };
        }

        public ApiPaging<ProductUnLockOutput> GetProductUnLock(PageInput input)
        {
            var query = _productLockRepository.GetAll()
                .Where(x => !x.ISDELETED);

            int total = query.Count();
            var pageData = query
                .OrderByDescending(x => x.CREATETIME)
                .ToPageList(input.PageIndex, input.PageSize);

            var output = _mapper.Map<List<ProductUnLockOutput>>(pageData);

            return new ApiPaging<ProductUnLockOutput>
            {
                Code = ApiEnums.Success,
                Message = "产品解锁分页数据获取成功",
                TotalCount = total,
                PageData = output
            };
        }
    }
}
